package com.qf.sclg.controller;

import com.qf.sclg.entity.SysUser;
import com.qf.sclg.result.ResponseCode;
import com.qf.sclg.result.ResponseData;
import com.qf.sclg.service.SysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * (SysUser)表控制层
 *
 * @author makejava
 * @since 2021-10-18 09:48:29
 */
@RestController
@RequestMapping("sysUser")
public class SysUserController {
    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;
    @ApiOperation(value = "微信登录")
    @GetMapping("/api/login/{code}")
    public ResponseData wxLogin(@PathVariable String code) throws IOException {
        String token = sysUserService.wxLogin(code);
        if(token != null){
            return new ResponseData(token);
        }else {
            return new ResponseData(ResponseCode.ERROR);
        }
    }
    @ApiOperation(value = "修改用户信息,如果不存在,则直接添加")
    @RequestMapping("/api/update")
    public ResponseData updateWxUserInfo(@RequestBody SysUser sysUser){
        Boolean update = sysUserService.update(sysUser);
        if (update){
            return new ResponseData();
        }else{
            return new ResponseData(ResponseCode.ERROR);
        }

    }
    @ApiOperation(value = "后台登录")
    @PostMapping("/login")
    public ResponseData sysLogin(@RequestBody SysUser sysUser){
        SysUser sysUser1 = sysUserService.sysLogin(sysUser);
        return new ResponseData(sysUser1);
    }
   
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<SysUser> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.sysUserService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param sysUser 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<SysUser> add(SysUser sysUser) {
        return ResponseEntity.ok(this.sysUserService.insert(sysUser));
    }

    /**
     * 编辑数据
     *
     * @param sysUser 实体
     * @return 编辑结果
     */
//    @PutMapping
//    public ResponseEntity<SysUser> edit(SysUser sysUser) {
//        return ResponseEntity.ok(this.sysUserService.update(sysUser));
//    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.sysUserService.deleteById(id));
    }

}

